<?php

namespace App\Models;


use App\Http\Traits\GlobalConstant;

class CourseSection extends Orm
{
    //
    protected $table = 'course_section';
    protected $dateFormat = 'U';
    public $timestamps = true;

    protected $appends = [
        'sk_time'
    ];

    public function getSkTimeAttribute()
    {
        $date = date('Y-m-d', $this->attributes['class_time']);
        return substr($date, 0, 10) . ' ' . GlobalConstant::daysOfWeekCn[date('l', strtotime($date))];
    }

    public function courseInfo()
    {
        return $this->belongsTo(Course::class, 'course_id')->withTrashed();
    }

    public function setClassTimeAttribute($value)
    {
        $this->attributes['class_time'] = strtotime($value);
    }

    public function getClassTimeAttribute()
    {
        return date('Y-m-d', $this->attributes['class_time']);
    }

    public function setBeginTimeAttribute($value)
    {
        $this->attributes['begin_time'] = GlobalConstant::formatAllTimeOrHour($this->attributes['class_time'], $value);
    }

    public function setEndTimeAttribute($value)
    {
        $this->attributes['end_time'] = GlobalConstant::formatAllTimeOrHour($this->attributes['class_time'], $value);
    }

    public function getBeginTimeAttribute()
    {
        return GlobalConstant::formatAllTimeOrHour($this->attributes['begin_time']);
    }

    public function getEndTimeAttribute()
    {
        return GlobalConstant::formatAllTimeOrHour($this->attributes['end_time']);
    }

}
